<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
       var arr = [
            { "caption": '生产工具/稿件平台', "path": 'toolOfProd/manuscriptPlatform', "page": 'manuscriptPlatform/index', "ordinal": 1 },
            { "caption": '生产工具/稿件平台1', "path": 'toolOfProd/manuscriptPlatform1', "page": 'manuscriptPlatform/index1', "ordinal": 12 },
            { "caption": '生产工具/稿件平台2', "path": 'toolOfProd/manuscriptPlatform2', "page": 'manuscriptPlatform/index2', "ordinal": 22 },
            { "caption": '生产工具/稿件平台2/稿件平台3', "path": 'toolOfProd/manuscriptPlatform3', "page": 'manuscriptPlatform/index3', "ordinal": 32 },
            { "caption": '市区通联/稿件详情', "path": 'toolOfProd/manuscriptDetail', "page": 'manuscriptPlatform/detail', "ordinal": 1 },
        ];
        var arr2 = []
        arr.forEach((item) => {
            let captionList = item.caption.split('/')
            let treeList = arr2
            captionList.forEach((caption,index) => {
                const existingCategory =  treeList.find( category => category.caption == caption )
                if(!existingCategory){
                    var newCategory = {
                        "caption":caption,
                        "path":item.path,
                        "page":item.page,
                        "ordinal":item.ordinal,
                        "children":[]
                    }
                    treeList.push(newCategory)
                    treeList = newCategory.children
                    console.log('a');
                }else{
                    console.log('b');
                    treeList = existingCategory.children
                    console.log(treeList);
                }
            })
        })
        console.log(arr2);
    </script>
</body>
</html>